package aftersale

import (
	"context"

	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteAfterSaleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 删除售后申请
func NewDeleteAfterSaleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAfterSaleLogic {
	return &DeleteAfterSaleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteAfterSaleLogic) DeleteAfterSale(req *types.DeleteAfterSaleReq) (resp *types.BaseMsgResp, err error) {
	if len(req.Ids) == 0 {
		return nil, resultx.NewErrCode(resultx.AFTER_SALE_PARAM_INVALID)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}
	_, err = l.svcCtx.OrderRpc.DeleteAfterSale(l.ctx, &orderclient.RemoveIDsReq{
		Ids:       req.Ids,
		VisitorId: pointy.GetPointer(userId),
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Msg: "删除成功",
	}, nil
}
